#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ERR_EXIT(m) \
 do \
 { \
 perror(m); \
 exit(EXIT_FAILURE); \
 } while(0)

 int main(int argc, char* argv[]){
    mkfifo("tp", 0644);
    int infd = open("abc.txt",O_RDONLY);
    if(infd == -1){
        ERR_EXIT("open");
    }
    int outfd;
    printf("diary1~\n");
    outfd = open("tp", O_WRONLY);
    if (outfd == -1) ERR_EXIT("open");
    int n;
    char buf[1024];
    printf("diary2~\n");
    while((n=read(infd, buf, 1024))>0){
        write(outfd, buf, n);
    }
    close(infd);
    close(outfd);

    /*
    memset(buf, 0x00, sizeof(buf));
    n = 0;
    int outfd1; outfd1 = open("abc.bak", O_WRONLY | O_CREAT | O_TRUNC, 0644); 
    if (outfd1 == -1) ERR_EXIT("open");

    int infd1;infd1 = open("tp", O_RDONLY);
    if (outfd1 == -1)ERR_EXIT("open");
                    
    
    while ((n=read(infd1, buf, 1024))>0)
    {
        printf("%d\n",n);
        write(outfd1, buf, n);
    }
    close(infd1);
    close(outfd1);
    unlink("tp");
    */
    return 0;
 }